<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="renderer" content="webkit">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>客户列表</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>

<script type="text/javascript">
	var prjId='${prjId}';
	var detailFlag = '${detailFlag}';
	var cstRlCd ='${cstRlCd}';
	var grid = null;
	$(function () {
	    initForm();
	    var editFlag = true;
	    if(detailFlag=="true"){
	    	$("#addBtn").hide();
	    	$("#removeBtn").hide();
	    	$("#saveBtn").hide();
		    grid = liger.get("ligergrid");
		    editFlag = false;
	    }
		initGrid(editFlag);
	    searchBtnEvent();
	    clearBtnEvent();
	    
	});
	
	//清空按钮事件
	function clearBtnEvent(){
		 $("#clearBtn").click(function(event) {
		        $("#resetBtn").click();  //重置 input-text
		        var form = liger.get("searchBar");
		        form.setData({  //重置select等特殊表单
		            "cstTypCd":" ",
		            "cstRlTyp":" ",
		            "searchCstNm": " ",
		        });
		 });
	}
	
	//初始化表单
	function initForm(){
		$("#searchBar").ligerForm();
	}
	
	//查询按钮事件
	function searchBtnEvent(){
		 $("#searchBtn").click(function(event) {
	         var form = new liger.get("searchBar");
	         var data = form.getData();
	         data["cstNm"]=$("#searchCstNm").val();
	         data["cstRlTyp"] = ProjectConstant.CST_RL_SPONSOR;
	         data["prjId"] = prjId;
	         for (name in data){
	            grid.setParm(name,data[name]);
	         };
	        grid.reload();
	    });
	}
	
	//查询参数
	function getSearchParams(){
		var form = new liger.get("searchBar");
        var data = form.getData();
        data["cstNm"]=$("#searchCstNm").val();
        data["cstRlTyp"] = ProjectConstant.CST_RL_SPONSOR;
        data["prjId"] = prjId;
        return data;
	}
	var guarTypCdList = [];
	//初始化列表
	function initGrid(editFlag){
		$.ajax({
			type: 'get',
			dataType:'json',
			data:'',
			url: '<%=path%>/project/prjcstbscInfo/getGuarTypCdList.do',
			success:function(msg){
		 if(msg.success == 1){
			 guarTypCdList = msg.data;
		$("#ligergrid").ligerGrid({
	    	url: '<%=path%>/project/prjcstbscInfo/initList.do',
	    	method : 'get',
	    	parms:getSearchParams(),
	        columns: [
	            { display: '客户ID', name: 'cstId', width: 50,align:'left', hide:true},
	            { display: '担保人名称', name: 'cstNm', minWidth: 140 , 
	            	render: function (rowdata, rowindex, value, column) { 
	            		var h = '';
	            		    h='<a class="sq-button-hasicon" href="javascript:detailBtn(';
	                        h+="'"+rowdata.cstId+"',";
	                        h+="'"+rowdata.cstTypCd+"'";
	                        h+=')">'+value+'</a>'
	                        return h;
	            		},
	            	enabledSort:true},
	            { display: '担保人类型', name: 'cstTypCd', width: 50,align:'left' , hide:true},
	            { display: '担保人类型', name: 'cstTypCdNm', width: 100,align:'left'},
	            { display: '证件/组织机构代码', name: 'certNo', minWidth: 140},
	            { display: '创建时间', name: 'crtTm', minWidth: 140 , type: 'date', render:renderDate},
	             { display: '担保方式', name: 'guarTypCdNm', minWidth: 140,
	            	},
	            	   {display: '最高保证金额(元)', name: 'guarAmt', minWidth: 140,
	                }, 
	                getOptColumn()
	        ],
            onBeforeSubmitEdit: function (e) {//重新赋值,要不然选中后离开会把key显示在前端
                if (e.selected && e.column.columnname == "guarTypCdNm") {
                    e.record.guarTypCdId = e.value;
                    e.value = e.selected[e.column.editor.textField];
                    e.record.guarTypCd = e.record.guarTypCdId;
                }
            },
	        pageSize:10,
	        root:'rows',
	        record:'total',
	        hideLoadButton : true,
	        width: '100%',
	        height:'100%',
	        heightDiff:-2,
	        checkbox:false,
	        enabledEdit: editFlag,
	        allowUnSelectRow: true,
            alternatingRow: false,
	        rownumbers:true,
	        enabledSort: false,
	    });
			    grid = liger.get("ligergrid");
			    grid.setHeight(100);
		 }else{
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
		 }
			}
		});
	}
	//获取操作列
	function getOptColumn(){
		var column = {
                display: '操作',
                isSort: false,
                width: 250,
                align:'left',
                render: function (rowdata, rowindex, value){
                	 var h="";
                		if(rowdata.id){
                		if((!detailFlag||detailFlag!="true")){
                        h+='<a class="sq-button-hasicon" href="javascript:removeBtn(';
                        h+="'"+rowdata.id+"'";
                        h+=')"><i class="sqBtnIco_7"></i>删除</a>'
                        
                        h+='<a class="sq-button-hasicon" href="javascript:modifyBtn(';
                        h+="'"+rowdata.cstId+"',";
                        h+="'"+rowdata.cstTypCd+"'";
                        h+=')"><i class="sqBtnIco_4"></i>修改</a>'
                        
                        h+='<a class="sq-button-hasicon" href="javascript:modifyInvestInfo(';
                        h+="'"+rowdata.cstId+"',";
                        h+="'"+rowdata.cstTypCd+"',";
                        h+="'"+rowdata.id+"',";
                        h+="'"+rowdata.cstRlCd+"'";
                        h+=')"><i class="sqBtnIco_4"></i>修改资信信息</a>'
                		}
                       var h2="";
               		   	  h2+='<a class="sq-button-hasicon" href="javascript:investInfoDetail(';
               		      h2+="'"+rowdata.cstId+"',";
                          h2+="'"+rowdata.cstTypCd+"',";
                          h2+="'"+rowdata.id+"',";
                          h2+="'"+rowdata.cstRlCd+"'";
                          h2+=')"><i class="sqBtnIco_2"></i>查看资信信息</a>';
                          
                          var hrefUrl = "<%=path%>/project/prjbscinfo/exportGuarInvesstRpt.do?prjId=${prjId}&&cstId="+rowdata.cstId;
                               hrefUrl+="&&cstTypCd="+rowdata.cstTypCd+"&&cstRlCd="+rowdata.cstRlCd+"&&prjCstId="+rowdata.id;
                       	       h2+='<a class="sq-button-hasicon" href="'+hrefUrl+'"><i class="sqBtnIco_21"></i>导出资信信息</a>'
                       	       h +=SQ.dropLinks(h2);
                       	       
                               var h3="";
                    		   	  h3+='<a class="sq-button-hasicon" href="javascript:investInfoDetail(';
                    		      h3+="'"+rowdata.cstId+"',";
                               h3+="'"+rowdata.cstTypCd+"',";
                               h3+="'"+rowdata.id+"',";
                               h3+="'"+rowdata.cstRlCd+"'";
                               h3+=')"><i class="sqBtnIco_2"></i>查看资信信息</a>';
                               
                               var hrefUrl = "<%=path%>/project/prjbscinfo/exportGuarInvesstRpt.do?prjId=${prjId}&&cstId="+rowdata.cstId;
                                    hrefUrl+="&&cstTypCd="+rowdata.cstTypCd+"&&cstRlCd="+rowdata.cstRlCd+"&&prjCstId="+rowdata.id;
                            	       h3+='<a class="sq-button-hasicon" href="'+hrefUrl+'"><i class="sqBtnIco_21"></i>导出资信信息</a>';
                       	       
                       	       if(detailFlag=="true"){
                       	    	   h=h3;
                       	       }
                		} 
                        	  return h;
                }
            }
		return column;
	}
	//修改担保人资信信息
	function modifyInvestInfo(cstId,cstTypCd,id,cstRlCd){
		var params = {};
			  params.cstId = cstId;
			  params.cstTypCd = cstTypCd;
			  params.prjId = prjId;
			  params.detailFlag = detailFlag;
			  params.cstRlCd = cstRlCd;
			  params.prjCstId = id;
		     Add_navTab('tab_guar_invest_modify_page'+cstId,'修改担保人资信信息','<%=path%>/project/prjcstempintvrec/toGuarListPage.do?cstId='+cstId+'&&cstTypCd='+cstTypCd+'&&prjId='+prjId+'&&detailFlag='+detailFlag+'&&cstRlCd='+cstRlCd+'&&prjCstId='+id,params);
	}
	
	function investInfoDetail(cstId,cstTypCd,id,cstRlCd){
		var params = {};
		  params.cstId = cstId;
		  params.cstTypCd = cstTypCd;
		  params.prjId = prjId;
		  params.detailFlag = true;
		  params.cstRlCd = cstRlCd;
		  params.prjCstId = id;
		  var url = '<%=path%>/project/prjcstempintvrec/toGuarListPage.do?cstId='+cstId+'&&cstTypCd='+cstTypCd+'&&prjId='+prjId+'&&detailFlag='+detailFlag+'&&cstRlCd='+cstRlCd+'&&prjCstId='+id;
		  var detailUrl = '<%=path%>/project/prjbscinfo/toPrjGuarDetailInvestRptPage.do?cstId='+cstId+'&&cstTypCd='+cstTypCd+'&&prjId='+prjId+'&&detailFlag='+detailFlag+'&&cstRlCd='+cstRlCd+'&&prjCstId='+id;
		 // Add_navTab('tab_guar_invest_modify_page'+cstId,'查看担保人资信信息',url,params);
		  Add_navTab('tab_guar_invest_modify_page'+cstId,'查看担保人资信信息',detailUrl,params);
	}
	
	function detailBtn(id,cstTypCd){
		if(cstTypCd==CustomerConstant.CST_TYPE_NP_CD){
			Add_navTab('tab_cst_detail'+id,'担保人详情','<%=path%>/customer/external/toCstDetailForPrjGuarCreditPage.do?id='+id);
		}
		if(cstTypCd==CustomerConstant.CST_TYPE_CORP_CD){
			Add_navTab('tab_cst_detail'+id,'担保人详情','<%=path%>/customer/external/toCstDetailForPrjGuarCreditPage.do?id='+id);
		}
	}
	
	
	function modifyBtn(id,cstTypCd){
		if(cstTypCd==CustomerConstant.CST_TYPE_NP_CD){
			Add_navTab('tab_cst_modify_page'+id,'修改担保人信息','<%=path%>/customer/external/toCstModifyForPrjGuarCreditPage.do?id='+id);
		}
		if(cstTypCd==CustomerConstant.CST_TYPE_CORP_CD){
			Add_navTab('tab_cst_modify_page'+id,'修改担保人信息','<%=path%>/customer/external/toCstModifyForPrjGuarCreditPage.do?id='+id);
		}
		
	}
	//格式化时间
	function renderDate(rowdata, index, value){
		var dateStr = "";
		if(null != value&&value!=''){
			var date = new Date(value);
			dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
		}
		return dateStr;
	}
	
	
	
	
	
	function checkAmt(amt){
		 var num = new Number(amt);
			var sAmt = parseFloat(amt).toString();
			return sAmt; 
	}
	//添加
	function addBtn(){
		var cstRlFlag = ProjectConstant.CST_RL_SPONSOR;//担保人
  		var win = $.ligerDialog.SQopen({ title: '添加担保人',height: 600,width: 850, url: '<%=path%>/project/prjbscinfo/toChoosePrjCstPage.do?cstRlFlag='+cstRlFlag, isResize: true });
	}
	function addRow(){
		if($("#cstId").val()){
			//添加担保人信息
 			var manager = $("#ligergrid").ligerGetGridManager();
            manager.addRow({
			cstId:$("#cstId").val(),
            cstStsCdNm:$("#cstStsCdNm").val(),
			cstLvlCdNm:$("#cstLvlCdNm").val(),
			cstNm:$("#cstNm").val(),
			cstTypCd:$("#cstTypCd").val(),
			cstTypCdNm:$("#cstTypCdNm").val(),
			certNo:$("#certNo").val(),
			crtTm:Number($("#crtTm").val()),
			guarTypCd:$("#guarTypCd").val(),
			guarTypCdNm:$("#guarTypCdNm").val(),
			guarAmt:checkAmt($("#guarAmt").val()),
            });
			}
	}
	
	function clearBtn(){
		var form = new liger.get("searchBar");
		form.clear();
	}
	
	function iframeResize () {
	    if (parent.mainApp) {
	        parent.mainApp.parentTab.onResize();
	    };
	}
	//删除所选项目客户信息
	function removeBtn(id){
		var _ids = [];
		$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
		if(id&&id!="undefined"){
			_ids.push(id);
		}else{
			var manager = $("#ligergrid").ligerGetGridManager();
			 manager.deleteSelectedRow();
			 return;
		}
			if(flag == false)return;
			$.ajax({
				type: 'post',
				dataType:'json',
				data:{ids:JSON.stringify(_ids)},
				url: '<%=path%>/project/prjcstbscInfo/removeByIds.do',
				success:function(msg){
					 if(msg.success == 1){
						 grid.reload();
					 }else{
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					 }
				}
			});
		});
		
	}
	//保存所选项目担保人信息
	function addSelect(guarList){
	/* 	var selectedRows = grid.getSelectedRows();
		if(selectedRows.length <1){
			$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
			return;
		} */
		//  var prjCstBscInfoList = grid.getData();
		/* var cstList =[];
        for ( var i=0; i<prjCstBscInfoList.length;i++) {
      	  var obj = prjCstBscInfoList[i];
      	  		if(obj.guarTypCdId){
      	  		obj.guarTypCd = obj.guarTypCdId;
      	  		}
      	  	  cstList.push(obj);
		  } */
		  var params = {};
		  params.prjId = prjId;
		  params.cstRlTyp =ProjectConstant.CST_RL_SPONSOR;   
		  params.prjCstBscInfo = JSON.stringify(guarList);
		     var wait_dialog;
			$.ajax({
				type: 'post',
				dataType:'json',
				data:params,
				url: '<%=path%>/project/prjcstbscInfo/addSelectPrjCstBscInfo.do',
				beforeSend: function(){
			          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			     },
				success:function(msg){
					 wait_dialog.close();
					 if(msg.success == 1){
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
						 grid.reload();
					 }else{
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					 }
				}
			});
	}
</script>

<body class="iframeBody">
<div class="Wrapper">
<div class="searchArea">
    <form id="searchBar">
        <div class="fieldset1 clearFix">
            <div class="fieldBox">
                <div class="label">担保人名称</div>
                <div class="field">
                    <input name="searchCstNm" id="searchCstNm" type="text" class="ui-textbox" value="">
                </div>
            </div>
            <div class="fieldBox">
                <div class="label">担保人类型</div>
                <div class="field">
                    <select name="cstTypCd" id="cstTypCd">
                        <option value=" " seleted>请选择</option>
                       <c:forEach items="${searchSelectCsttypcdlist }" var="csttypcd">
                        	<option value="${csttypcd.key }">${csttypcd.value }</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
          <div class="clearFix"></div>
            <div class="btnsBox">
               <span id="searchBtn" class="sq-button sq-button-round">查询</span>
               <span id="clearBtn" class="sq-button sq-button-round">清空</span>
               <span id="addBtn" onclick="addBtn();" class="sq-button sq-button-round">新增</span>
        <!--       <span id="removeBtn" onclick="removeBtn();" class="sq-button sq-button-round">作废</span> -->
<!--               <span id="saveBtn" class="sq-button sq-button-round" onclick="addSelect()"><i class="sqBtnIco_10"></i>保存</span>
 -->            </div>
        </div>
        </form>
        <div style="display: none">
             <input type="hidden"  id="cstId" name="cstId"  class="ui-textbox" value="${cstId}">
            <input type="hidden"  id="prjId" name="prjId"  class="ui-textbox" value="${prjId}">
            <input type="hidden"  id="cstStsCdNm" name="cstStsCdNm"  class="ui-textbox" value="">
            <input type="hidden"  id="cstLvlCdNm" name="cstLvlCdNm"  class="ui-textbox" value="">
            <input type="hidden"  id="cstNm" name="cstNm"  class="ui-textbox" value="">
            <input type="hidden"  id="cstTypCd" name="cstTypCd"  class="ui-textbox" value="">
            <input type="hidden"  id="cstTypCdNm" name="cstTypCdNm"  class="ui-textbox" value="">
            <input type="hidden"  id="certNo" name="certNo"  class="ui-textbox" value="">
            <input type="hidden"  id="crtTm" name="crtTm"  class="ui-textbox" value="">
             <input type="hidden"  id="guarTypCdNm" name="guarTypCdNm"  class="ui-textbox" value="">
              <input type="hidden"  id="guarTypCd" name="guarTypCd"  class="ui-textbox" value="">
            <input type="hidden"  id="guarAmt" name="guarAmt"  class="ui-textbox" value="">
        </div>
    </div>
		<div id="ligergrid" class="sq-grid"></div>
</div>
</body>
</html>